package com.android.test.view;

import com.android.test.R;
import com.android.test.model.Entry;
import com.android.test.model.General;
import com.android.test.model.IconPair;
import com.android.test.model.OrgDetailInfo;
import com.android.test.model.OrgInfo;
import com.android.test.util.IconCache;
import com.android.test.util.NumberUtil;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class DetailView extends RelativeLayout implements General {

    private Context mContext;

    private Handler mHandler;
    private ImageView mOrgImage;
    private ImageView mOrgSnap;
    private TextView mOrgDetail;
    private OrgDetailInfo mOrgDetailInfo;
    private TextView mDownloadBtn;
    private TextView mOrgAddr;
    private TextView mOrgDownload;

    public DetailView(Context context, AttributeSet attrs, int defStyle) {
	super(context, attrs, defStyle);
	mContext = context;
	init();
    }

    public DetailView(Context context, AttributeSet attrs) {
	this(context, attrs, 0);
    }

    public DetailView(Context context) {
	this(context, null);
    }

    private void init() {

	mHandler = new Handler() {
	    @Override
	    public void handleMessage(Message msg) {
		if (msg == null) {
		    return;
		}
		switch (msg.what) {/*
				    * case -1000: int index = msg.arg1; int
				    * OrgId = msg.arg2; if (OrgId !=
				    * mOrg.getOrgId()) { return; } if (msg.obj
				    * != null && msg.obj instanceof String) {
				    * IconPair iconPair =
				    * IconCache.getInstance().get((String)
				    * msg.obj); if (index == 203) {
				    * mOrgImage.setImageDrawable
				    * (iconPair.drawable); }else if (index ==
				    * 502) {
				    * mOrgSnap.setImageDrawable(iconPair.drawable
				    * ); } } break; case
				    * Downloads.STATUS_RUNNING: if
				    * (mOrg.getViewType() ==
				    * Entry.VIEW_TYPE_LIST_ITEM) { if (msg.arg2
				    * == msg.arg1) {
				    * mOrg.setDownloadType(Org.Org_STATUS_DOWNLOADED
				    * ); mDownloadBtn.setTag("downloaded");
				    * mDownloadBtn.setTag(-1 + "-" + (String)
				    * mDownloadBtn.getTag());
				    * mDownloadBtn.setBackgroundResource
				    * (R.drawable.icon_result_downloaded);
				    * DownloadManager
				    * .unregisterUpdateListener(mOrg
				    * .getOrgId()); }
				    * mItemProgress.setMax(msg.arg1);
				    * mItemProgress.setProgress(msg.arg2); }else
				    * if (mOrg.getViewType() ==
				    * Entry.VIEW_TYPE_GALLERY_ITEM) { if
				    * (msg.arg2 == msg.arg1) {
				    * mOrg.setDownloadType
				    * (Org.Org_STATUS_DOWNLOADED);
				    * mOrgDownload.setTag("downloaded");
				    * mOrgDownload.setTag(-1 + "-" + (String)
				    * mOrgDownload.getTag()); String
				    * downloadCount =
				    * mContext.getResources().getString
				    * (R.string.try_download_count,
				    * mOrgNativeDetail.getDownloadCount());
				    * String download =
				    * mContext.getString(R.string
				    * .try_download_setup) + "\n"; Spannable
				    * span = new SpannableString(download +
				    * downloadCount); span.setSpan(new
				    * AbsoluteSizeSpan(14), download.length(),
				    * download.length() +
				    * downloadCount.length(),
				    * Spanned.SPAN_INCLUSIVE_INCLUSIVE);
				    * mOrgDownload.setText(span); //
				    * mOrgDownload
				    * .setText(R.string.try_download_setup);
				    * DownloadManager
				    * .unregisterUpdateListener(mOrg
				    * .getOrgId()); } } break; case
				    * ActivitySearchResult.MSG_DOWNLOAD_START:
				    * if (mOrg.getViewType() ==
				    * Entry.VIEW_TYPE_LIST_ITEM) {
				    * mDownloadBtn.setBackgroundResource
				    * (R.drawable.icon_result_downloading);
				    * }else if (mOrg.getViewType() ==
				    * Entry.VIEW_TYPE_GALLERY_ITEM) { String
				    * downloadCount =
				    * mContext.getResources().getString
				    * (R.string.try_download_count,
				    * mOrgNativeDetail.getDownloadCount());
				    * String download =
				    * mContext.getString(R.string
				    * .try_download_downloading) + "\n";
				    * Spannable span = new
				    * SpannableString(download + downloadCount);
				    * span.setSpan(new AbsoluteSizeSpan(14),
				    * download.length(), download.length() +
				    * downloadCount.length(),
				    * Spanned.SPAN_INCLUSIVE_INCLUSIVE);
				    * mOrgDownload.setText(span); //
				    * mOrgDownload
				    * .setText(R.string.try_download_downloading
				    * ); } break; case
				    * HttpManager.MSGCODE_HTTP_ERROR:
				    * 
				    * break; case
				    * HttpManager.MSGCODE_HTTP_RESPONSE:
				    * QueuedRequest qr = (QueuedRequest)
				    * msg.obj; if(qr.requestId == 203) {
				    * 
				    * }else if(qr.requestId == 501) {
				    * mOrgNativeDetail =
				    * JsonParser.getOrgNativeDetail
				    * ((String)qr.result); if (mOrgNativeDetail
				    * == null) { return; }
				    * if(mOrgNativeDetail.snapshotUrls != null
				    * && mOrgNativeDetail.snapshotUrls.length >=
				    * 1) { mOrgDetail.setText(mOrgNativeDetail.
				    * description); String downloadCount =
				    * mContext
				    * .getResources().getString(R.string.
				    * try_download_count,
				    * mOrgNativeDetail.getDownloadCount());
				    * String download =
				    * mOrgDownload.getText().toString() + "\n";
				    * Spannable span = new
				    * SpannableString(download + downloadCount);
				    * span.setSpan(new AbsoluteSizeSpan(14),
				    * download.length(), download.length() +
				    * downloadCount.length(),
				    * Spanned.SPAN_INCLUSIVE_INCLUSIVE);
				    * mOrgDownload.setText(span); //
				    * mOrgDownloadCount
				    * .setText(mContext.getResources
				    * ().getString(R.string.try_download_count,
				    * mOrgNativeDetail.getDownloadCount()));
				    * mOrgVersion
				    * .setText(mContext.getString(R.string
				    * .try_Org_version,
				    * mOrgNativeDetail.getVersionName()));
				    * displayIcon(mOrgSnap, mOrgNativeDetail,
				    * false); }
				    * 
				    * }else if(qr.requestId == 502) {
				    * 
				    * } break; }
				    */
		}
	    }

	};
    }

    /*
     * private void setItemData(final OrgInfo Org) { this.removeAllViews();
     * RelativeLayout view = (RelativeLayout) LayoutInflater.from(mContext)
     * .inflate(R.layout.org_detail_view ,null); mOrgImage = (ImageView)
     * view.findViewById(R.id.item_icon);
     * 
     * ((TextView) view.findViewById(R.id.item_name))
     * .setText(Org.getOrgName()); ((TextView)
     * view.findViewById(R.id.item_size)).setText(String
     * .valueOf(Org.getSizeStr())); mOrgDetail = (TextView)
     * view.findViewById(R.id.item_market); mOrgDetail.setText(Org.getMarket());
     * this.addView(view); mDownloadBtn = (TextView)
     * view.findViewById(R.id.item_operation);
     * 
     * mDownloadBtn.setTag(-1 + "-" + (String) mDownloadBtn.getTag());
     * mDownloadBtn.setOnClickListener(new View.OnClickListener() {
     * 
     * public void onClick(View v) { String tagVal = (String) v.getTag();
     * String[] vals = tagVal.split("-"); int position =
     * NumberUtil.toInt(vals[0]); String action = vals[2];
     * 
     * 
     * if(StringUtil.equals(action, "download")) { DownloadManager.getInstance
     * (mContext).startDownload(Org.getDownloadUrl(), Org.getDownloadType(),
     * Org.getOrgId(), Org.getPackageName(), Org.getOrgName(), Org.getSize());
     * 
     * Message msg = Message.obtain(); msg.what =
     * ActivitySearchResult.MSG_DOWNLOAD_START; msg.obj = position;
     * mHandler.sendMessageDelayed(msg, 1000);
     * DownloadManager.registerUpdateListener(Org.getOrgId(), mHandler); } else
     * if(StringUtil.equals(action, "downloading")){
     * GlobalUtil.shortToast(mContext, R.string.download_started_msg); } else
     * if(StringUtil.equals(action, "downloaded")){ DownloadManager.getInstance
     * (mContext).startDownload(Org.getDownloadUrl(), Org.getDownloadType(),
     * Org.getOrgId(), Org.getPackageName(), Org.getOrgName(), Org.getSize()); }
     * 
     * } }); displayIcon(mOrgImage, Org, true); }
     */

    private void setData(final OrgDetailInfo Org) {
	this.removeAllViews();
	RelativeLayout view = (RelativeLayout) LayoutInflater.from(mContext)
		.inflate(R.layout.org_detail_view, null);
	ScrollView scrollView = (ScrollView) view
		.findViewById(R.id.scroll_layout);
	mOrgImage = (ImageView) view.findViewById(R.id.org_icon);
	mOrgSnap = (ImageView) view.findViewById(R.id.try_org_snapshot);
	mOrgSnap.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {

		Intent intent = new Intent();
		/*
		 * intent.setClassName(mContext,
		 * OrgSnapshotBrowse.class.getName());
		 */
		intent.putExtra("Org", Org);
		mContext.startActivity(intent);
	    }
	});

	((TextView) view.findViewById(R.id.try_org_title)).setText(Org
		.shortName);
	((TextView) view.findViewById(R.id.try_org_url)).setText(mContext
		.getString(R.string.try_org_url, Org.Url));
	((TextView) view.findViewById(R.id.try_org_tel)).setText(mContext
		.getString(R.string.try_org_tel, Org.getTel()));
	mOrgDownload = (TextView) view.findViewById(R.id.try_org_download_btn);
	TextView grade = (TextView) view.findViewById(R.id.org_grade);
	String points = mContext.getString(R.string.try_org_grade_point);
	String gradeText = grade.getText().toString();
	Spannable span = new SpannableString(gradeText + points);
	span.setSpan(new AbsoluteSizeSpan(14), gradeText.length(),
		gradeText.length() + points.length(),
		Spanned.SPAN_INCLUSIVE_INCLUSIVE);
	grade.setText(span);
	mOrgAddr = (TextView) view.findViewById(R.id.try_org_address);
	mOrgAddr.setText(Org.mainAddress);
	mOrgDetail = (TextView) view.findViewById(R.id.org_intro_content);
	mOrgDetail.setText(Org.description);
	this.addView(view);
	final Button loadMoreDetail = (Button) view
		.findViewById(R.id.try_org_load_more_detail_btn);
	loadMoreDetail.setTag("close_intro");
	loadMoreDetail.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		int ii = mOrgDetail.getLineCount();
		if (loadMoreDetail.getTag().equals("close_intro")) {
		    if (ii <= 3) {
			return;
		    }
		    loadMoreDetail.setTag("open_intro");
		    mOrgDetail.setMaxLines(100);
		    loadMoreDetail.setText(R.string.try_close_all_intro);
		} else {
		    loadMoreDetail.setTag("close_intro");
		    loadMoreDetail.setText(R.string.try_all_intro);
		    mOrgDetail.setMaxLines(3);
		    mOrgDetail.setText(mOrgDetail.getText());
		}
	    }
	});
	mOrgDownload.setTag(-1 + "-" + (String) mOrgDownload.getTag());
	mOrgDownload.setOnClickListener(new View.OnClickListener() {

	    public void onClick(View v) {
		String tagVal = (String) v.getTag();
		String[] vals = tagVal.split("-");
		int position = NumberUtil.toInt(vals[0]);
		String action = vals[2];

		/*
		 * if(StringUtil.equals(action, "download")) {
		 * DownloadManager.getInstance
		 * (mContext).startDownload(Org.getDownloadUrl(),
		 * Org.getDownloadType(), Org.getOrgId(), Org.getPackageName(),
		 * Org.getOrgName(), Org.getSize());
		 * 
		 * Message msg = Message.obtain(); msg.what =
		 * ActivitySearchResult.MSG_DOWNLOAD_START; msg.obj = position;
		 * mHandler.sendMessageDelayed(msg, 1000);
		 * DownloadManager.registerUpdateListener(Org.getOrgId(),
		 * mHandler); } else if(StringUtil.equals(action,
		 * "downloading")){ GlobalUtil.shortToast(mContext,
		 * R.string.download_started_msg); } else
		 * if(StringUtil.equals(action, "downloaded")){
		 * DownloadManager.getInstance
		 * (mContext).startDownload(Org.getDownloadUrl(),
		 * Org.getDownloadType(), Org.getOrgId(), Org.getPackageName(),
		 * Org.getOrgName(), Org.getSize()); }
		 */
	    }
	});
	/*
	 * HttpService.getInstance(mContext).requestOrgDetail(GlobalConstants.
	 * TYPE_ID_NATIVE_DETAIL, Org.getOrgId(), 501, mHandler);
	 * displayIcon(mOrgImage, Org, true); if (mOrgNativeDetail != null) {
	 * displayIcon(mOrgSnap, mOrgNativeDetail, false); }
	 */
    }

    private void displayIcon(final ImageView imageview, OrgInfo Org,
	    boolean isOrgIcon) {
	if (Org == null) {
	    return;
	}
	IconPair iconPair = IconCache.getInstance().get(Org._id);
	if (iconPair != null) {
	    final Drawable drawable = iconPair.getDrawable();
	    if (drawable != null) {
		imageview.setImageDrawable(drawable);
	    } else {
		imageview.setImageResource(isOrgIcon ? R.drawable.icon_default
			: R.drawable.bg_about);
/*		IconCache.addListener(this);*/
	    }
	}
    }

    public void setData(Entry entry) {
	if (entry == null) {
	    return;
	}
	if (entry instanceof OrgDetailInfo) {
	    final OrgInfo mOrg = (OrgInfo) entry;
	    if (entry.getViewType() == Entry.VIEW_TYPE_LIST_ITEM) {
       //Oops,should never happen
	    } else if (entry.getViewType() == Entry.VIEW_TYPE_GALLERY_ITEM) {
		setData((OrgDetailInfo) entry);
	    }
	}
    }

    public View getDetailView() {
	return this;
    }

    public void setPosition(int position, int count) {
	// ((TextView)findViewById(R.id.Org_position)).setText(mContext.getString(R.string.item_view_position,
	// position, count));
    }


}